﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Cheetah_Fast_Banking_App
{
    public partial class BillPayForm : Form
    {
        private BillPay _billPay;

        public string Description
        {
            get { return DescriptionTextBox.Text; }
            set { DescriptionTextBox.Text = value; }
        }

        public decimal Amount
        {
            get { return Decimal.Parse(AmountTextBox.Text); }
            set { AmountTextBox.Text = value.ToString(); }
        }

        public DateTime FirstDate
        {
            get
            {
                DateTime date = FirstDateTime.Value;
                return new DateTime(date.Year, date.Month, date.Day);
            }
            set { FirstDateTime.Value = new DateTime(value.Year, value.Month, value.Day); }
        }

        public bool HasLastDate
        {
            get { return LastCheckBox.Checked; }
            set { LastCheckBox.Checked = value; }
        }

        public DateTime LastDate
        {
            get
            {
                DateTime date = LastDateTime.Value;
                return new DateTime(date.Year, date.Month, date.Day);
            }
            set { LastDateTime.Value = new DateTime(value.Year, value.Month, value.Day); }
        }

        public Interval Interval
        {
            get { return (Interval)Enum.Parse(typeof(Interval), IntervalComboBox.SelectedItem.ToString()); }
            set
            {
                int index = IntervalComboBox.Items.IndexOf(value.ToString());
                IntervalComboBox.SelectedIndex = index;
            }
        }

        public bool Active
        {
            get { return ActiveCheckBox.Checked; }
            set { ActiveCheckBox.Checked = value; }
        }

        public BillPayForm()
            : this(null)
        { }

        public BillPayForm(BillPay billPay)
        {
            InitializeComponent();
            this._billPay = billPay;
            LoadIntervalComboBox();
            IntervalComboBox.SelectedIndex = 0;
            LoadBillPay();
            LastDateTime.Enabled = HasLastDate;
        }

        private void LoadIntervalComboBox()
        {
            foreach (string freq in Enum.GetNames(typeof(Interval)))
            {
                IntervalComboBox.Items.Add(freq);
            }
        }

        private void LoadBillPay()
        {
            if (this._billPay == null)
                return;

            this.Description = _billPay.Description;
            this.FirstDate = _billPay.FirstDate;
            this.HasLastDate = !_billPay.LastDate.HasValue;
            this.LastDate = _billPay.LastDate.HasValue ? _billPay.LastDate.Value : DateTime.Today;
            this.Amount = _billPay.Amount;
            this.Active = _billPay.Active;
        }

        public BillPay GetBillPay()
        {
            return _billPay;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            DateTime? lastDate = null;
            lastDate = HasLastDate ? LastDate : lastDate;

            if (_billPay == null)
            {
                _billPay = new BillPay(Description, FirstDate, lastDate, Active, Interval, Amount);
            }
            else
            {
                _billPay.Active = this.Active;
                _billPay.Interval = this.Interval;
                _billPay.Description = this.Description;
                _billPay.FirstDate = this.FirstDate;
                _billPay.LastDate = lastDate;
                _billPay.Amount = this.Amount;
            }

            this.DialogResult = DialogResult.OK;

            this.Hide();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LastCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            LastDateTime.Enabled = HasLastDate;
        }
    }
}
